package com.laijiale.controller;

import cn.hutool.core.bean.BeanUtil;
import com.laijiale.Service.AssetsService;
import com.laijiale.dto.PageDTO;
import com.laijiale.dto.AssetsInsertDTO;
import com.laijiale.dto.AssetsPageDTO;
import com.laijiale.dto.AssetsUpdateDTO;
import com.laijiale.entities.Assets;

import com.laijiale.entities.Assets;
import com.laijiale.excel.AssetsExcel;
import com.laijiale.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 11:59
 */
@Tag(name = "资产控制器")
@RestController
@RequestMapping("/api/v1/assets")
public class AssetsController {
    @Autowired
    private AssetsService assetsService;
    @Operation(summary = "添加一条记录")
    @PostMapping("save")
    public Object save(@RequestBody  @Validated AssetsInsertDTO assetsInsertDTO){

        return assetsService.insert(assetsInsertDTO);
    }
    @Operation(summary = "查询一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return assetsService.selectById(id);
    }
    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated AssetsUpdateDTO assetsUpdateDTO){

        return assetsService.updateById(assetsUpdateDTO);

    }

    @Operation(summary = "删除一条记录")
    @PostMapping("remove/{id}")
    public Object remove(@PathVariable Long id){

        return assetsService.logicDeleteById(id);

    }
    @Operation(summary = "批量删除记录")
    @PostMapping("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){

        return assetsService.logicDeleteByIds(ids);

    }


    @Operation(summary = "全查询记录")
    @GetMapping("list")
    public Object list(){

        return assetsService.list();

    }
    @Operation(summary = "分页查询记录")
    @PostMapping("page")
    public Object page(@RequestBody  @Validated PageDTO pageDTO){

        return assetsService.page(pageDTO);

    }
    /*
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<AssetsExcel> assetsExcels = new ArrayList<>();
        List<Assets> assetss = assetsService.list();
        for (Assets assets : assetss) {
            AssetsExcel assetsExcel = BeanUtil.copyProperties(assets, AssetsExcel.class);
            assetsExcels.add(assetsExcel);
        }
        EasyExcelUtil.download(resp, "资产统计表.xlsx", "sheet01",
                AssetsExcel.class, assetsExcels);
    }

     */
    @Operation(summary = "按资产名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated AssetsPageDTO dto){
        return assetsService.pageLikeTitle(dto);
    }
   
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<AssetsExcel> assetsExcels = new ArrayList<>();
        List<Assets> assetss = assetsService.list();
        for (Assets assets: assetss) {
            AssetsExcel assetsExcel = BeanUtil.copyProperties(assets, AssetsExcel.class);
            assetsExcels.add(assetsExcel);
        }
        EasyExcelUtil.download(resp, "资产统计表.xlsx", "sheet01",
                AssetsExcel.class, assetsExcels);
    }


}
